//
//  SYYTools.m
//  NineteenBook
//
//  Created by xhkj on 2021/8/11.
//  Copyright © 2021 UP. All rights reserved.
//

#import "SYYTools.h"
#import <sys/utsname.h>
#import "GetUUID.h"


@implementation SYYTools

/*
 * 获取资源文件
 */
+ (UIImage *)getImageForName:(NSString *)name
{
    NSBundle * currentBundle = [NSBundle bundleForClass:self];
    NSURL*url = [currentBundle URLForResource:@"SYYBaseLib" withExtension:@"bundle"];
    if (!url) {
        return nil;
    }
    NSBundle*targetBundle = [NSBundle bundleWithURL:url];
    UIImage *image = [UIImage imageNamed:name inBundle:targetBundle compatibleWithTraitCollection:nil];
        
    return image;
}




/**
 * 世界时间转换为本地时间 减去 8h
 */
+(NSDate *)conversionWorldDateToLocalDate:(NSDate *)date
{
    //获取本地时区(中国时区)
    NSTimeZone* localTimeZone = [NSTimeZone localTimeZone];
    
    //计算世界时间与本地时区的时间偏差值
    NSInteger offset = -[localTimeZone secondsFromGMTForDate:date];
    
    //世界时间＋偏差值 得出中国区时间
    NSDate *localDate = [date dateByAddingTimeInterval:offset];
    
    return localDate;
}
/**
 * 秒 -> 时间@"yyyy-MM-dd HH:mm:ss"
 */
+(NSString *)conversionFormatWithTimeInterval:(NSTimeInterval)timeInterval format:(NSString *)format
{
    //用[NSDate date]可以获取系统当前时间
    NSDate *detailDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:format];
    NSString * detailDateStr = [dateFormatter stringFromDate:detailDate];
    return detailDateStr;
}
/**
 * 毫秒 -> 时间@"yyyy-MM-dd HH:mm:ss"
 */
+(NSString *)conversionFormatWithMSTimeInterval:(NSTimeInterval)timeInterval format:(NSString *)format
{
    //用[NSDate date]可以获取系统当前时间
    NSDate *detailDate = [NSDate dateWithTimeIntervalSince1970:(timeInterval/1000.0)];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:format];
    NSString * detailDateStr = [dateFormatter stringFromDate:detailDate];
    return detailDateStr;
}
/**
 * 获取当前时间@"yyyy-MM-dd HH:mm:ss"
 */
+(NSString *)conversionCurrentDateWithFormat:(NSString *)format
{
    //    1. 获取当前系统的准确事件(+8小时)
    
    //    NSDate *date = [NSDate date]; // 获得时间对象
    //
    //    NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
    //
    //    NSTimeInterval time = [zone secondsFromGMTForDate:date];// 以秒为单位返回当前时间与系统格林尼治时间的差
    //
    //    NSDate *dateNow = [date dateByAddingTimeInterval:time];// 然后把差的时间加上,就是当前系统准确的时间
    
    //    2. 获取当前系统事件并设置格式  @"HH-mm-ss yyyy-MM-dd"
    NSDate *date = [NSDate date]; // 获得时间对象
    return [self conversionFormatWithDate:date format:format];
}
/**
 * 时间转换格式 @"yyyy-MM-dd_HHmmss" -> @"yyyy-MM-dd HH:mm:ss"
 */
+(NSString *)conversionFormatWithDateStr:(NSString *)dateStr oldFormat:(NSString *)oldFormat newFormat:(NSString *)newFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = oldFormat;
    NSDate *date = [dateFormatter dateFromString:dateStr];
    return [self conversionFormatWithDate:date format:newFormat];
}
/**
 * NSDate -> NSString @"yyyy-MM-dd HH:mm:ss"
 */
+(NSString *)conversionFormatWithDate:(NSDate *)date format:(NSString *)format
{
    //指定时间显示样式: HH表示24小时制 hh表示12小时制
    NSDateFormatter *forMatter = [[NSDateFormatter alloc] init];
    [forMatter setDateFormat:format];
    return [forMatter stringFromDate:date];
}
/**
 * NSString @"yyyy-MM-dd HH:mm:ss" -> NSDate
 */
+(NSDate *)conversionDateFromTime:(NSString *)timeStr format:(NSString *)format
{
    NSDateFormatter *forMatter = [[NSDateFormatter alloc] init];
    //指定时间显示样式: HH表示24小时制 hh表示12小时制
    [forMatter setDateFormat:format];//@"yyyy-MM-dd HH:mm:ss"
    //[forMatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];//解决8小时时间差问题

    NSDate *date = [forMatter dateFromString:timeStr];
    return date;
}
/**
 * 时间Str @"yyyy-MM-dd HH:mm:ss" -> 时间戳 秒
 */
+(double)conversionTimeIntervalFromTime:(NSString *)timeStr format:(NSString *)format
{
    NSDate *lastDate = [self conversionDateFromTime:timeStr format:format];
    //以 1970/01/01 GMT为基准，得到lastDate的时间戳
    double timestamp = [self conversionTimeIntervalFromDate:lastDate];
    
    return timestamp;
}
/**
 * 时间NSDate -> 时间戳 秒
 */
+(double)conversionTimeIntervalFromDate:(NSDate *)timeDate
{
    //以 1970/01/01 GMT为基准，得到lastDate的时间戳
    double timestamp = [timeDate timeIntervalSince1970];
    
    //    //以 lastTime GMT为基准，得到的时间戳
    //    long secondStamp = [[NSDate date] timeIntervalSinceDate:lastDate];
    //    //以 此时 GMT为基准，得到lastDate的时间戳
    //    long thirdStamp = [lastDate timeIntervalSinceNow];
    //    //以 2001/01/01 GMT为基准，得到的时间戳
    //    long fourthStamp = [[NSDate date] timeIntervalSinceReferenceDate];
    
    return timestamp;
}
/**
 * 时间Str @"yyyy-MM-dd HH:mm:ss" -> 时间戳 毫秒
 */
+(double)conversionMSTimeIntervalFromTime:(NSString *)timeStr format:(NSString *)format
{
    //时间转换成时间戳
    NSDate *lastDate = [self conversionDateFromTime:timeStr format:format];
    //以 1970/01/01 GMT为基准，得到lastDate的时间戳
    double timestamp = [self conversionMSTimeIntervalFromDate:lastDate];
    
    return timestamp;
}
/**
 * 时间NSDate -> 时间戳 毫秒
 */
+(double)conversionMSTimeIntervalFromDate:(NSDate *)timeDate
{
    //以 1970/01/01 GMT为基准，得到lastDate的时间戳
    double timestamp = [timeDate timeIntervalSince1970] * 1000;
    return timestamp;
}
/**
 * 毫秒转天数 距离现在多少天
 */
+(NSInteger)getDaysWithMTimeInterval:(double)MTimeInterval
{
    double currentMInterval = [self conversionTimeIntervalFromDate:[NSDate date]] * 1000;
    double saveMTimeInterval = currentMInterval - MTimeInterval;
    NSInteger daysCount = saveMTimeInterval / 86400000;
    return daysCount;
}
/**
 * 两个时间NSDate之间的秒数
 */
+ (double)getTimeIntervalWithDate:(NSDate *)date anotherDate:(NSDate *)anotherDate
{
    double timestamp = [date timeIntervalSince1970];
    double antoherTimestamp = [anotherDate timeIntervalSince1970];
    return antoherTimestamp - timestamp;
}












//生成渐变图
+ (UIImage *)getBGImage:(UIColor *)startColor and:(UIColor *)endColor directionType:(NSInteger)directionType option:(CGSize)size
{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)startColor.CGColor,
                             (__bridge id)endColor.CGColor];
    gradientLayer.locations = @[@(0.0f), @(1.0f)];
    
    if (directionType == 1) { //AC - BD
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
    }else if (directionType == 2){//AB - CD
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(0, 1);
    }else if (directionType == 3){//A - D
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 1);
    }else if (directionType == 4){//C - B
        gradientLayer.startPoint = CGPointMake(0, 1);
        gradientLayer.endPoint = CGPointMake(1, 0);
    }
    
    
    gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(gradientLayer.frame.size, NO, 0);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return gradientImage;
}





/**
 * 操作按钮 原生
 */
+ (void)popAlertViewWithTitle:(NSString *)title message:(NSString *)message alertTitles:(NSArray *)alertTitles sourceVC:(UIViewController *)sourceVC clickBlock:(void(^)(NSInteger index))clickBlock
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    for (NSInteger i = 0; i < alertTitles.count; i++) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:alertTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
            if (clickBlock) {
                clickBlock(i);
            }
        }];
        [alertController addAction:action];
    }
    [sourceVC presentViewController:alertController animated:YES completion:nil];
}

/**
 * 操作按钮 底部 原生
 */
+ (void)popAlertBottomViewWithTitles:(NSArray *)titles sourceVC:(UIViewController *)sourceVC clickBlock:(void(^)(NSInteger index))clickBlock
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    for (NSInteger i = 0; i < titles.count; i++) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:titles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
            if (clickBlock) {
                clickBlock(i);
            }
        }];
        [alertController addAction:action];
    }
    
    //取消:style:UIAlertActionStyleCancel
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    
    [sourceVC presentViewController:alertController animated:YES completion:nil];
}



/**
 * 得到top部 BaseViewController 控制器
 */
//+ (HHParentVC *)getAppTopVC
//{
//    HHTabBarController *tabBarController =(HHTabBarController*)[UIApplication sharedApplication].keyWindow.rootViewController;
//    if (![tabBarController isKindOfClass:[HHTabBarController class]]) {
//        return nil;
//    }
//    HHBaseNavVC *naVC = tabBarController.selectedViewController;
//    HHParentVC *topVC = (HHParentVC *)naVC.topViewController;
//    return topVC;
//}


/**
 * 得到top部 BaseNavigationController 导航控制器
 */
//+ (HHBaseNavVC *)getAppTopNaVC
//{
//    HHTabBarController *tabBarController = (HHTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//    HHBaseNavVC *naVC = tabBarController.selectedViewController;
//    return naVC;
//}


/**
 * 得到top部 UPTabbarVC
 */
//+ (HHTabBarController *)getAppTabbarVC
//{
//    HHTabBarController *tabBarController = (HHTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//    return tabBarController;
//}












+ (void)jumpToWebViewWithURL:(NSURL *)URL requst:(NSURLRequest *)requst titleStr:(NSString *)titleStr sourceNaVC:(UINavigationController *)sourceNaVC
{
//    WKWebViewController *wkWebVC = [[WKWebViewController alloc]init];
//    wkWebVC.URL = URL;
//    wkWebVC.requst = requst;
//    wkWebVC.titleStr = titleStr;
//    if ([sourceNaVC isKindOfClass:[UINavigationController class]]) {
//         [sourceNaVC pushViewController:wkWebVC animated:YES];
//    }else{
//        [sourceNaVC.navigationController pushViewController:wkWebVC animated:YES];
//    }
//   
}









#pragma mark - 获取当前版本号
+ (NSString *)getCurrentVersionStr
{
    NSMutableString *currentVersion = [NSMutableString stringWithString:[NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"]];//version
    return currentVersion;
}
+ (NSInteger)getCurrentVersion
{
    // 获取当前版本号
    NSMutableString *oldVersion = [NSMutableString stringWithString:[NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"]];
    for (int i = 0 ; i < oldVersion.length; i++) {
        NSRange range = NSMakeRange(i, 1);
        NSString *characterStr = [oldVersion substringWithRange:range];
        if ([characterStr isEqualToString:@"."]) {
            [oldVersion deleteCharactersInRange:range];
        }
    }
    NSInteger oldVersionNum = [oldVersion integerValue];
    return oldVersionNum;
}
#pragma mark -- 系统版本
+ (NSString *)getPhoneVersion
{
    return [[UIDevice currentDevice] systemVersion];
}
#pragma mark -- 手机型号
//需要#import <sys/utsname.h>
+ (NSString *)getiphoneType
{
    // 获取设备标识Identifier
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
    // iPhone
    if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
    if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
    if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";
    if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";
    if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s";
    if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s";
    if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
    if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
    if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
    if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
    if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
    if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
    if ([platform isEqualToString:@"iPhone10,1"]) return @"iPhone 8";
    if ([platform isEqualToString:@"iPhone10,4"]) return @"iPhone 8";
    if ([platform isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus";
    if ([platform isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus";
    if ([platform isEqualToString:@"iPhone10,3"]) return @"iPhone X";
    if ([platform isEqualToString:@"iPhone10,6"]) return @"iPhone X";
    if ([platform isEqualToString:@"iPhone11,2"]) return @"iPhone XS";
    if ([platform isEqualToString:@"iPhone11,6"]) return @"iPhone XS MAX";
    if ([platform isEqualToString:@"iPhone11,8"]) return @"iPhone XR";
    if ([platform isEqualToString:@"iPhone12,1"]) return @"iPhone 11";
    if ([platform isEqualToString:@"iPhone12,3"]) return @"iPhone 11 Pro";
    if ([platform isEqualToString:@"iPhone12,5"]) return @"iPhone 11 Pro Max";
    if ([platform isEqualToString:@"iPhone12,8"]) return @"iPhone SE 2";

    
    
    // iPod
    if ([platform isEqualToString:@"iPod1,1"])  return @"iPod Touch 1";
    if ([platform isEqualToString:@"iPod2,1"])  return @"iPod Touch 2";
    if ([platform isEqualToString:@"iPod3,1"])  return @"iPod Touch 3";
    if ([platform isEqualToString:@"iPod4,1"])  return @"iPod Touch 4";
    if ([platform isEqualToString:@"iPod5,1"])  return @"iPod Touch 5";
    if ([platform isEqualToString:@"iPod7,1"])  return @"iPod Touch 6";
    if ([platform isEqualToString:@"iPod9,1"])  return @"iPod Touch 7";
    
    // iPad
    if ([platform isEqualToString:@"iPad1,1"])  return @"iPad 1";
    if ([platform isEqualToString:@"iPad2,1"])  return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,3"])  return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,4"])  return @"iPad 2";
    if ([platform isEqualToString:@"iPad2,5"])  return @"iPad Mini 1";
    if ([platform isEqualToString:@"iPad2,6"])  return @"iPad Mini 1";
    if ([platform isEqualToString:@"iPad2,7"])  return @"iPad Mini 1";
    if ([platform isEqualToString:@"iPad3,1"])  return @"iPad 3";
    if ([platform isEqualToString:@"iPad3,2"])  return @"iPad 3";
    if ([platform isEqualToString:@"iPad3,3"])  return @"iPad 3";
    if ([platform isEqualToString:@"iPad3,4"])  return @"iPad 4";
    if ([platform isEqualToString:@"iPad3,5"])  return @"iPad 4";
    if ([platform isEqualToString:@"iPad3,6"])  return @"iPad 4";
    if ([platform isEqualToString:@"iPad4,1"])  return @"iPad Air";
    if ([platform isEqualToString:@"iPad4,2"])  return @"iPad Air";
    if ([platform isEqualToString:@"iPad4,3"])  return @"iPad Air";
    if ([platform isEqualToString:@"iPad4,4"])  return @"iPad Mini 2";
    if ([platform isEqualToString:@"iPad4,5"])  return @"iPad Mini 2";
    if ([platform isEqualToString:@"iPad4,6"])  return @"iPad Mini 2";
    if ([platform isEqualToString:@"iPad4,7"])  return @"iPad mini 3";
    if ([platform isEqualToString:@"iPad4,8"])  return @"iPad mini 3";
    if ([platform isEqualToString:@"iPad4,8"])  return @"iPad mini 3";
    if ([platform isEqualToString:@"iPad4,9"])  return @"iPad mini 4";
    if ([platform isEqualToString:@"iPad5,2"])  return @"iPad mini 4";
    if ([platform isEqualToString:@"iPad5,3"])  return @"iPad Air 2";
    if ([platform isEqualToString:@"iPad5,4"])  return @"iPad Air 2";
    if ([platform isEqualToString:@"iPad6,3"])  return @"iPad Pro (9.7-inch)";
    if ([platform isEqualToString:@"iPad6,4"])  return @"iPad Pro (9.7-inch)";
    if ([platform isEqualToString:@"iPad6,7"])  return @"iPad Pro (12.9-inch)";
    if ([platform isEqualToString:@"iPad6,8"])  return @"iPad Pro (12.9-inch)";
    if ([platform isEqualToString:@"iPad6,11"])  return @"iPad 5";
    if ([platform isEqualToString:@"iPad6,12"])  return @"iPad 5";
    if ([platform isEqualToString:@"iPad7,1"])  return @"iPad Pro 2(12.9-inch)";
    if ([platform isEqualToString:@"iPad7,2"])  return @"iPad Pro 2(12.9-inch)";
    if ([platform isEqualToString:@"iPad7,3"])  return @"iPad Pro (10.5-inch)";
    if ([platform isEqualToString:@"iPad7,4"])  return @"iPad Pro (10.5-inch)";
    if ([platform isEqualToString:@"iPad7,5"])  return @"iPad 6";
    if ([platform isEqualToString:@"iPad7,6"])  return @"iPad 6";
    if ([platform isEqualToString:@"iPad7,11"])  return @"iPad 7";
    if ([platform isEqualToString:@"iPad7,12"])  return @"iPad 7";
    if ([platform isEqualToString:@"iPad8,1"])  return @"iPad Pro (11-inch) ";
    if ([platform isEqualToString:@"iPad8,2"])  return @"iPad Pro (11-inch) ";
    if ([platform isEqualToString:@"iPad8,3"])  return @"iPad Pro (11-inch) ";
    if ([platform isEqualToString:@"iPad8,4"])  return @"iPad Pro (11-inch) ";
    if ([platform isEqualToString:@"iPad8,5"])  return @"iPad Pro 3 (12.9-inch) ";
    if ([platform isEqualToString:@"iPad8,6"])  return @"iPad Pro 3 (12.9-inch) ";
    if ([platform isEqualToString:@"iPad8,7"])  return @"iPad Pro 3 (12.9-inch) ";
    if ([platform isEqualToString:@"iPad8,8"])  return @"iPad Pro 3 (12.9-inch) ";
    if ([platform isEqualToString:@"iPad11,1"])  return @"iPad mini 5";
    if ([platform isEqualToString:@"iPad11,2"])  return @"iPad mini 5";
    if ([platform isEqualToString:@"iPad11,3"])  return @"iPad Air 3";
    if ([platform isEqualToString:@"iPad11,4"])  return @"iPad Air 3";
    
    // 其他
    if ([platform isEqualToString:@"i386"])   return @"iPhone Simulator";
    if ([platform isEqualToString:@"x86_64"])  return @"iPhone Simulator";
    
    return platform;
}

#pragma mark -- 手机唯一标识 UUID
+ (NSString *)getPhoneUUID{
    return [GetUUID getUUID];
}

//如果想要判断组织是ipad，要用如下方法
+ (BOOL)getIsIpad
{
    NSString *deviceType = [UIDevice currentDevice].model;
    
    if([deviceType isEqualToString:@"iPhone"]) {
        //iPhone
        return NO;
    }
    else if([deviceType isEqualToString:@"iPod touch"]) {
        //iPod Touch
        return NO;
    }
    else if([deviceType isEqualToString:@"iPad"]) {
        //iPad
        return YES;
    }
    return NO;
}








@end
